package com.gitee.wsl.jvm.reflect.autobox


import com.gitee.wsl.jvm.reflect.ReflectionUtils.cast
import java.math.BigDecimal

class FloatTypeAutoboxer : TypeAutoboxer<Float> {
    override fun supports(type: Class<*>): Boolean {
        return Float::class.javaPrimitiveType == type || Float::class.java == type
    }

    override fun autobox(value: Any, type: Class<Float>): Float {
        if (value is Float) {
            return cast(value)
        } else if (value is BigDecimal) {
            return cast(value.toFloat())
        } else if (value is String) {
            return cast(value.toLong())
        }
        throw UnsupportedOperationException(
            String.format(
                "Cannot autobox %s of type %s to %s",
                value,
                value.javaClass.name,
                Long::class.java.name
            )
        )
    }
}
